<template>
  <div>
    <div style="margin: 10px;">
      <el-button type="primary" @click="handleAdd">增加管理员</el-button>
    </div>
    <div style="margin: 10px;">
      <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="userName"
            label="登录账号"
            width="180">
          </el-table-column>
          <el-table-column
            prop="nickName"
            label="昵称"
            width="180">
          </el-table-column>
          <el-table-column
            prop="realName"
            label="真实姓名">
          </el-table-column>
          <el-table-column
            prop="mobile"
            label="联系电话">
          </el-table-column>
          <el-table-column
            prop="userEmail"
            label="电子邮箱">
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间">
          </el-table-column>
          <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
              </el-table-column>
        </el-table>
    </div>

    <div style="margin: 10px;">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="pageNum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        >
      </el-pagination>
    </div>

    <div>
      <el-dialog title="添加管理员" :visible.sync="dialogFormVisible" width="30%">
        <el-form ref="addForm" :model="form" :rules="rules">
          <el-form-item label="登录账号" prop="userName" :label-width="formLabelWidth">
            <el-input v-model="form.userName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="登录密码" prop="userPass1" :label-width="formLabelWidth">
            <el-input v-model="form.userPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="userPass2" :label-width="formLabelWidth">
            <el-input v-model="form.userPass2" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="真实姓名" prop="realName" :label-width="formLabelWidth">
            <el-input v-model="form.realName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="mobile" :label-width="formLabelWidth">
            <el-input v-model="form.mobile" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱" :label-width="formLabelWidth">
            <el-input v-model="form.userEmail" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号昵称" :label-width="formLabelWidth">
            <el-input v-model="form.nickName" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitAdd">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { getAdminList,addAdmin } from '@/api/admin'
  export default {
    name: 'Admin',
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入确认密码'));
        } else if (value !== this.form.userPass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      return {
        tableData: [],
        pageNum: 1,
        pageSize: 2,
        total: 0,
        formLabelWidth: '120px',
        dialogFormVisible: false,
        form: {
          userName: '',
          userPass: '',
          userPass2: '',
          realName: '',
          mobile: '',
          userEmail: '',
          nickName: ''
        },
        rules: {
          userName: [{ required: true, message: '请输入登录账号', trigger: 'blur' }],
          userPass: [{ required: true, message: '请输入登录密码', trigger: 'blur' }],
          userPass2: [{ validator: validatePass, trigger: 'blur' }],
          realName: [{ required: true, message: '请输入真实姓名', trigger: 'blur' }],
          mobile: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        }
      }
    },
    mounted() {
      this.loadList()
    },
    methods: {
      loadList() {
        let param = {
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
        getAdminList(param).then(res => {
          console.log('getAdminList', res);
          if(res.code == 200) {
            this.tableData = res.data.records
            this.pageNum = res.data.current
            this.pageSize = res.data.size
            this.total = res.data.total
          }
        })
      },
      // 删除管理员
      handleDelete(index, row) {
        console.log('handleDelete', row);
      },
      // 点击添加
      handleAdd() {
        this.dialogFormVisible = true
      },
      // 提交添加
      submitAdd() {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            addAdmin(this.form).then(res => {
              if(res.code == 200) {
                this.dialogFormVisible = false
                this.$message.success('添加成功')
                this.loadList()
              }
              else {
                let msg = res.msg
                this.$message.error(msg)
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val
        this.loadList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.pageNum = val
        this.loadList()
      }
    }
  }
</script>

<style>
</style>
